﻿using System.Collections.Generic;
using System;
using System.ComponentModel.Design;
using UnityEngine.Analytics;

public partial class RequestServerController
{
    public void RequestUserMomentInfo(long userId, Action<UserMomentsInfo> callback)
    {
        GameDebug.Log($"RequestUserMomentInfo:{userId}");

        Request_GetUserMomentsInfo request = new Request_GetUserMomentsInfo();
        request.getUserMomentsReqData.userId = userId;

        request.onResponse = delegate (ResponseBase response)
        {
            GetUserMomentsInfoResponseData fetchMoments = response.responseData as GetUserMomentsInfoResponseData;

            GameDebug.Log(userId+"  Request_GetUserMomentsInfo onResponse:" + fetchMoments.userMomentsInfo?.moments?.Length);

            if (response.responseData.code == 0)
            {
                callback?.Invoke(fetchMoments.userMomentsInfo);
            }
            else
            {
                MGame.instance.ShowNoticePanel("网络出错，请重试");
                callback?.Invoke(null);
            }
        };
        request.onTimeOut = delegate (RequestBase req)
        {
            GameDebug.Log("Request_GetUserMomentsInfo onTimeOut ");
            callback?.Invoke(null);
        };
        request.clientSocket = MGame.instance.clientSocket;
        request.timeOutSeconds = 12;
        request.Send();
    }

    //MomentsData 
    public void ReqMomentsData(int type, int beginIndex, int num,int gender,Action<MomentsData> callback)
    {
        Request_FetchMoments request = new Request_FetchMoments();
        request.fetchUserMomentsReqData.num = num;
        request.fetchUserMomentsReqData.communityType = type;
        request.fetchUserMomentsReqData.beginIndex = beginIndex;
        request.fetchUserMomentsReqData.gender = gender;

        request.onResponse = delegate (ResponseBase response)
        {
            FetchMomentsResponseData fetchMoments = response.responseData as FetchMomentsResponseData;

            GameDebug.Log("Request_FetchMoments onResponse:" + fetchMoments.moments?.Count);

            if (response.responseData.code == 0)
            {
                MomentsData data = new MomentsData();
                data.userMoments = fetchMoments.moments;

                callback?.Invoke(data);
            }
            else
            {
                MGame.instance.ShowNoticePanel("网络出错，请重试");
                callback?.Invoke(null);
            }
        };
        request.onTimeOut = delegate (RequestBase req)
        {
            GameDebug.Log("Request_FetchMoments onTimeOut ");
            callback?.Invoke(null);
        };

        request.clientSocket = MGame.instance.clientSocket;
        request.timeOutSeconds = 12;
        request.Send();
    }

    public void ReqAddMoment(long userId,long time, string content, string location, string topic,
        int momentStatus,Tex[] texs, Action<Moment> callback)
    {
        Request_AddMoment request = new Request_AddMoment();
        request.addMomentReqData.userId = userId;
        request.addMomentReqData.time = time;
        request.addMomentReqData.content = content;
        request.addMomentReqData.location = location;
        request.addMomentReqData.topic = topic;
        request.addMomentReqData.momentStatus = momentStatus;
        request.addMomentReqData.texs = texs;
          
        request.onResponse = delegate (ResponseBase response)
        {
            GameDebug.Log("Request_AddMoment onResponse:" + response.responseData.code);

            AddMomentResponseData addMomentRes = response.responseData as AddMomentResponseData;

            if (response.responseData.code == 0)
            {
                MGame.instance.MUser.AddMoment(addMomentRes.moment);

                callback?.Invoke(addMomentRes.moment);
            }
            else
            {
                MGame.instance.ShowNoticePanel("网络出错，请重试");
                callback?.Invoke(null);
            }
        };
        request.onTimeOut = delegate (RequestBase req)
        {
            GameDebug.Log("Request_AddMoment onTimeOut ");
            callback?.Invoke(null);
        };

        request.clientSocket = MGame.instance.clientSocket;
        request.timeOutSeconds = 12;
        request.Send();
    }

    public void ReqFollow(long fromUserId, long toUserId, bool followOrNot,Action<long> callback)
    {
        Request_Follow request = new Request_Follow();
        request.followReqData.fromUserId = fromUserId;
        request.followReqData.toUserId = toUserId;
        request.followReqData.followOrNot = followOrNot;

        request.onResponse = delegate (ResponseBase response)
        {
            GameDebug.Log("Request_Follow  onResponse:" + response.responseData.code);

            if (response.responseData.code == 0)
            {
                if(followOrNot)
                    MGame.instance.MUser.AddMyFollow(toUserId);
                else
                    MGame.instance.MUser.RemoveMyFollow(toUserId);

                callback?.Invoke(toUserId);
            }
            else
            {
                MGame.instance.ShowNoticePanel("网络出错，请重试");
                callback?.Invoke(-1);
            }
        };
        request.onTimeOut = delegate (RequestBase req)
        {
            GameDebug.Log("Request_Follow onTimeOut ");
            callback?.Invoke(-1);
        };
        request.clientSocket = MGame.instance.clientSocket;
        request.timeOutSeconds = 12;
        request.Send();
    }
    public void ReqLikeMoment(long fromUserId,bool likeOrNot, long momentUserId, long momentId,Action<int> callback)
    {
        Request_LikeMoment request = new Request_LikeMoment();
        request.likeReqData.momentId = momentId;
        request.likeReqData.momentUserId = momentUserId;
        request.likeReqData.likeOrNot = likeOrNot;

        request.onResponse = delegate (ResponseBase response)
        {
            GameDebug.Log("Request_Like  onResponse:" + response.responseData.code);

            if (response.responseData.code == 0)
            {
                //if(momentUserId == MGame.instance.MUser.id)
                //{
                //    var moment = MGame.instance.MUser.moments.Find(e => e.momentId == momentId);
                //    moment.AddLike(fromUserId);
                //}

                callback?.Invoke(0);
            }
            else
            {
                MGame.instance.ShowNoticePanel("网络出错，请重试");
                callback?.Invoke(-1);
            }
        };
        request.onTimeOut = delegate (RequestBase req)
        {
            GameDebug.Log("Request_Like onTimeOut ");
            callback?.Invoke(-1);
        };

        request.clientSocket = MGame.instance.clientSocket;
        request.timeOutSeconds = 12;
        request.Send();
    }

    public void ReqLikeComment(long fromUserId,bool likeOrNot, long momentUserId,long momentId, long commentUserId, long commentId, Action<int> callback)
    {
        Request_LikeComment request = new Request_LikeComment();
        request.likeReqData.momentUserId = momentUserId;
        request.likeReqData.momentId = momentId;
        request.likeReqData.commentUserId = commentUserId;
        request.likeReqData.commentId = commentId;
        request.likeReqData.likeOrNot = likeOrNot;

        request.onResponse = delegate (ResponseBase response)
        {
            GameDebug.Log("Request_LikeComment  onResponse:" + response.responseData.code);

            if (response.responseData.code == 0)
            {
                callback?.Invoke(0);
            }
            else
            {
                MGame.instance.ShowNoticePanel("网络出错，请重试");
                callback?.Invoke(-1);
            }
        };
        request.onTimeOut = delegate (RequestBase req)
        {
            GameDebug.Log("Request_LikeComment onTimeOut ");
            callback?.Invoke(-1);
        };

        request.clientSocket = MGame.instance.clientSocket;
        request.timeOutSeconds = 12;
        request.Send();
    }

    public void ReqAddComment(long userId, long momentUserId, long momentId, string msg,Action<Comment> callback)
    {
        Request_AddComment request = new Request_AddComment();
        request.addCommentReqData.momentUserId = momentUserId;
        request.addCommentReqData.momentId = momentId;
        request.addCommentReqData.content = msg;

        request.onResponse = delegate (ResponseBase response)
        {
            if (response.responseData.code == 0)
            {
                AddCommentResponseData commentRes = response.responseData as AddCommentResponseData;
                callback?.Invoke(commentRes.comment);
                GameDebug.Log("Request_AddComment  onResponse:" + commentRes.comment?.commentId);
            }
            else
            {
                MGame.instance.ShowNoticePanel("网络出错，请重试");
                callback?.Invoke(null);
            }
        };
        request.onTimeOut = delegate (RequestBase req)
        {
            GameDebug.Log("Request_AddComment onTimeOut ");
            callback?.Invoke(null);
        };

        request.clientSocket = MGame.instance.clientSocket;
        request.timeOutSeconds = 12;
        request.Send();
    }
}
